//
//  GMGridView.h
//  GMGridView
//
//  Created by Gulam Moledina on 11-10-09.
//  Copyright (C) 2011 by Gulam Moledina.
//
//  Latest code can be found on GitHub: https://github.com/gmoledina/GMGridView
// 
//  Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
//  The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
//

#import <UIKit/UIKit.h>
#import "GMGridView-Constants.h"
#import "GMGridViewCell.h"

@protocol GMGridViewDataSource;
@protocol GMGridViewDelegate;
@protocol GMGridViewSortingDelegate;
@protocol GMGridViewLayoutStrategy;

typedef enum
{
    GMGridViewStylePush = 0,
    GMGridViewStyleSwap
} GMGridViewStyle;


//////////////////////////////////////////////////////////////
#pragma mark Interface GMGridView
//////////////////////////////////////////////////////////////

@interface GMGridView : UIView
{
    
}

// Delegates
@property (nonatomic, assign) NSObject<GMGridViewDataSource> *dataSource;                    // Required
@property (nonatomic, assign) NSObject<GMGridViewDelegate> *actionDelegate;            // Optional - to get taps callback
@property (nonatomic, assign) NSObject<GMGridViewSortingDelegate> *sortingDelegate;          // Optional - to enable sorting

// Layout Strategy
@property (nonatomic, retain) id<GMGridViewLayoutStrategy> layoutStrategy; // Default is GMGridViewLayoutVerticalStrategy

// Editing Mode
@property (nonatomic, getter=isEditing) BOOL editing; // Default is NO - When set to YES, all gestures are disabled and delete buttons shows up on cells

// Customizing Options
@property (nonatomic, assign) UIView *mainSuperView;                 // Default is self
@property (nonatomic) GMGridViewStyle style;                          // Default is GMGridViewStyleSwap
@property (nonatomic) NSInteger itemHorizontalSpacing;                // Default is 10
@property (nonatomic) NSInteger itemVerticalSpacing;                  // Default is 10
@property (nonatomic) BOOL centerGrid;                                // Default is YES
@property (nonatomic) UIEdgeInsets minEdgeInsets;                     // Default is (5, 5, 5, 5)
@property (nonatomic) CFTimeInterval minimumPressDuration;            // Default is 0.2; if set to 0, the scrollView will not be scrollable
@property (nonatomic) BOOL showFullSizeViewWithAlphaWhenTransforming; // Default is YES - not working right now
@property (nonatomic) BOOL showsVerticalScrollIndicator;              // Default is YES
@property (nonatomic) BOOL showsHorizontalScrollIndicator;            // Default is YES

@property (nonatomic) BOOL isDestop;            // Default is YES
@property (nonatomic, readonly) UIScrollView *scrollView;             // Messing with the scrollView can lead to unexpected behavior. Avoid changing any properties
                                                                      // or changing its delegate. You have been warned.



// Reusable cells
- (GMGridViewCell *)dequeueReusableCell;                              // Should be called in GMGridView:cellForItemAtIndex: to reuse a cell

// Cells
- (GMGridViewCell *)cellForItemAtIndex:(NSInteger)position;           // Might return nil if cell not loaded for the specific index

// Actions
- (void)reloadData;
- (void)endEditingStatus;
- (void)insertObjectAtIndex:(NSInteger)index;
- (void)removeObjectAtIndex:(NSInteger)index;
- (void)reloadObjectAtIndex:(NSInteger)index;
- (void)swapObjectAtIndex:(NSInteger)index1 withObjectAtIndex:(NSInteger)index2;
- (void)scrollToObjectAtIndex:(NSInteger)index animated:(BOOL)animated;
- (void)scrollRectToVisiblePageAtIndex:(NSInteger)index animated:(BOOL)animated;

@end


#pragma mark - Protocol GMGridViewDataSource
@protocol GMGridViewDataSource <NSObject>

@required
// Populating subview items 
- (NSInteger)numberOfItemsInGMGridView:(GMGridView *)gridView;
- (CGSize)sizeForItemsInGMGridView:(GMGridView *)gridView;
- (GMGridViewCell *)gmGridView:(GMGridView *)gridView cellForItemAtIndex:(NSInteger)index;

@optional
// Required to enable editing mode
- (void)gmGridView:(GMGridView *)gridView deleteItemAtIndex:(NSInteger)index;

@end


#pragma mark - Protocol GMGridViewActionDelegate
@protocol GMGridViewDelegate <NSObject>
@optional
- (void)gmGridView:(GMGridView *)gridView didSelectItemAtIndex:(NSInteger)index;
- (void)gmGridViewDidEndEditing:(GMGridView *)gridView;
- (void)gmGridView:(GMGridView *)gridView pageNumberInGridView:(NSInteger)pageNumber;

@end


#pragma mark - Protocol GMGridViewSortingDelegate
@protocol GMGridViewSortingDelegate <NSObject>
@optional
// Sorting started/ended - indexes are not specified on purpose (not the right place to update data structure)
- (void)gmGridView:(GMGridView *)gridView didStartMovingCell:(GMGridViewCell *)cell;
- (void)gmGridView:(GMGridView *)gridView didEndMovingCell:(GMGridViewCell *)cell;
// Enable/Disable the shaking behavior of an item being moved
- (BOOL)gmGridView:(GMGridView *)gridView shouldAllowShakingBehaviorWhenMovingCell:(GMGridViewCell *)view atIndex:(NSInteger)index;

// Item moved - right place to update the data structure
- (void)gmGridView:(GMGridView *)gridView moveItemAtIndex:(NSInteger)oldIndex toIndex:(NSInteger)newIndex;
- (void)gmGridView:(GMGridView *)gridView exchangeItemAtIndex:(NSInteger)index1 withItemAtIndex:(NSInteger)index2;

@end

